-------------------------------------------------------
-- 头像&头像框

-- head = nil,    							-- 头像
-- headList = nil, 							-- 已激活头像列表（除默认赠送外）
-- headFrame = nil,                     	-- 头像框
-- headFrameList = nil,                  	-- 已激活头像框列表（除默认赠送外）
-- body = nil,								-- 形象
-- bodyList = nil,							-- 已激活形象
-- chenghao = nil,							-- 称号
-- chenghaoList = nil,						-- 已激活称号
-- headHasNewFlag = nil,					-- 头像1/头像框2/形象4/称号8有新的flag

--------------------------------------------------------
local Util = require("common.Util")
local Lang = require("common.Lang")
local Log = require("common.Log")
local LogDefine = require("common.LogDefine")
local RoleExcel = require("excel.role")
local HeroExcel = require("excel.hero")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local RoleSystemLogic = require("roleSystem.RoleSystemLogic")
local RoleSystemDefine = require("roleSystem.RoleSystemDefine")
local Broadcast = require("broadcast.Broadcast")
local CombatPosLogic = require("combat.CombatPosLogic")
local CombatDefine = require("combat.CombatDefine")
local ChenghaoImp = require("role.ChenghaoImp")
local RoleDBLogic = require("role.RoleDBLogic")
local RoleDefine = require("role.RoleDefine")
local RoleAttr = require("role.RoleAttr")
local ObjHumam = require("core.ObjHuman")
local HuanJingTowerLogic = require("huanjingTower.HuanjingTowerLogic")
local HeroLogic = require("hero.HeroLogic")
local TheStarsDBLogic = require("theStars.TheStarsDBLogic")

HEAD_TYPE_1 = 1 -- 头像
HEAD_TYPE_2 = 2 -- 头像框
HEAD_TYPE_3 = 3 -- 形象
HEAD_TYPE_4 = 4 -- 称号
HEAD_TYPE_5 = 5 -- 立绘
HEAD_TYPE_6 = 6 -- 背景

DEFAULT_FREE_ACTIVE_TYPE = 2	-- 默认激活类型

DEFAULT_HEAD_MALE_ID = 1	-- 默认男性头像id
DEFAULT_HEAD_FEMALE_ID = 2  -- 默认女性头像id

DEFAULT_HEADFREAM_ID = 6000 -- 默认头像框id
DEFAULT_BACKGROUND_ID = 1010 -- 默认背景id

DEFAULT_DEL_TYPE = 99

-- 背景解锁 类型 九零  一 起玩 www.9  01 7 5.com 
BACKGROUND_TYPE_4 =  4      -- 战役通关X关

local function getConfig(headType, id)
	if headType == HEAD_TYPE_1 then
		return RoleExcel.head[id]
	elseif 	headType == HEAD_TYPE_2 then
		return RoleExcel.headFrame[id] 
	elseif 	headType == HEAD_TYPE_3 then
		return RoleExcel.body[id] 
	elseif 	headType == HEAD_TYPE_4 then
		return RoleExcel.chenghao[id]
	elseif 	headType == HEAD_TYPE_5 then
		return RoleExcel.body[id]
	elseif 	headType == HEAD_TYPE_6 then
		return RoleExcel.background[id]
	end
end


function isActive(human, headType, id)
    local config = nil
	local checkDB = nil
	if headType == HEAD_TYPE_1 then -- 头像
        config = RoleExcel.head[id]
		if id == DEFAULT_HEAD_MALE_ID or 
			id == DEFAULT_HEAD_FEMALE_ID or
            config.type == DEFAULT_FREE_ACTIVE_TYPE then
			return true
		end
		if not human.db.headList then
			return
		end	
		checkDB = human.db.headList[id]
	elseif headType == HEAD_TYPE_2 then -- 头像框
        config = RoleExcel.headFrame[id]
		if id == DEFAULT_HEADFREAM_ID or
           config.type == DEFAULT_FREE_ACTIVE_TYPE then
			return true
		end
		if not human.db.headFrameList then
			return
		end	
		checkDB = human.db.headFrameList[id]
	elseif headType == HEAD_TYPE_3 then -- 形象
        config = RoleExcel.body[id]
        if config.type == DEFAULT_FREE_ACTIVE_TYPE then
			return true
		end

		if not human.db.bodyList then
			return
		end	
		checkDB = human.db.bodyList[id]
	elseif headType == HEAD_TYPE_4 then -- 称号
        config = RoleExcel.chenghao[id]
        if config.type == DEFAULT_FREE_ACTIVE_TYPE then
			return true
		end

		if not human.db.chenghaoList then
			return
		end
		checkDB = human.db.chenghaoList[id]
	elseif headType == HEAD_TYPE_5 then -- 立绘
        config = RoleExcel.body[id]
        if config.type == DEFAULT_FREE_ACTIVE_TYPE then
			return true
		end

		if not human.db.animationList then
			return
		end	
		checkDB = human.db.animationList[id]
	elseif headType == HEAD_TYPE_6 then -- 背景
        config = RoleExcel.background[id]
        if config.type == DEFAULT_FREE_ACTIVE_TYPE or
            id == DEFAULT_BACKGROUND_ID then
			return true
		elseif config.type == BACKGROUND_TYPE_4 then
			if human.db.battleID >= config.need then
				return true
			end
		end

		if not human.db.backgroundList then
			return
		end
		checkDB = human.db.backgroundList[id]
	end
	
	if checkDB == nil then
		return
	end
	
	if checkDB == true then
		return true
	end
	
	if checkDB.keepTime < 0 or (checkDB.ts + checkDB.keepTime > os.time()) then
		return true
	else
		-- 过期了
		unActive(human.db._id, headType, id)
	end
end

function active(human, headType, id)
	local conf = getConfig(headType, id)
	if conf == nil then
		return
	end

    local keepTime = conf.keepTime * 60*60        -- conf.keepTime  为小时数
	if isActive(human, headType, id) then
		if headType == HEAD_TYPE_4 and keepTime > 0 then
		else
			return
		end		
	end

	if headType == HEAD_TYPE_1 then -- 头像
		human.db.headList = human.db.headList or {}
		if keepTime == nil then
			human.db.headList[id] = true
		else
			human.db.headList[id] = {ts = os.time(), keepTime = keepTime}
		end
	elseif headType == HEAD_TYPE_2 then -- 头像框
		human.db.headFrameList = human.db.headFrameList or {}
		if keepTime == nil then
			human.db.headFrameList[id] = true
		else
			human.db.headFrameList[id] = {ts = os.time(), keepTime = keepTime}
		end
	elseif headType == HEAD_TYPE_3 then -- 形象
		human.db.bodyList = human.db.bodyList or {}
		if keepTime == nil then
			human.db.bodyList[id] = true
		else
			human.db.bodyList[id] = {ts = os.time(), keepTime = keepTime}
		end
        if human.db.body == nil then
            human.db.body = id
            -- 通知客户端
		    local msgRet = Msg.gc.GC_ROLE_HEAD_SET
		    msgRet.headID = id
		    msgRet.headType = headType
		    Msg.send(msgRet, human.fd)
        end
	elseif headType == HEAD_TYPE_4 then -- 称号
		if conf.group ~= 0 then
			for k, v in pairs(RoleExcel.chenghao) do
				if k ~= id and v.group == conf.group then
					unActive(human.db._id, HEAD_TYPE_4, k)
				end
			end
		end
		
		human.db.chenghaoList = human.db.chenghaoList or {}
		if keepTime == nil then
			human.db.chenghaoList[id] = true
		else
			human.db.chenghaoList[id] = {ts = os.time(), keepTime = keepTime}
		end
    elseif headType == HEAD_TYPE_5 then -- 立绘
		human.db.animationList = human.db.animationList or {}
		if keepTime == nil then
			human.db.animationList[id] = true
		else
			human.db.animationList[id] = {ts = os.time(), keepTime = keepTime}
		end
        if human.db.animation == nil then
            human.db.animation = id
            -- 通知客户端
		    local msgRet = Msg.gc.GC_ROLE_HEAD_SET
		    msgRet.headID = id
		    msgRet.headType = headType
		    Msg.send(msgRet, human.fd)
        end
	elseif headType == HEAD_TYPE_6 then -- 背景
		human.db.backgroundList = human.db.backgroundList or {}
		if keepTime == nil then
			human.db.backgroundList[id] = true
		else
			human.db.backgroundList[id] = {ts = os.time(), keepTime = keepTime}
		end
	end
	
	setNewFlag(human, headType, true)

	if #conf.attrs > 0 then
		ObjHuman.doCalcHero(human)
	end

	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_101)
end

function unActive(uuid, headType, id)
	local changed = nil
    local human = ObjHumam.onlineUuid[uuid]
	if headType == HEAD_TYPE_1 then -- 头像
		local db = RoleDBLogic.getDb(uuid,{head = 1, headList = 1})
		if db.headList and db.headList[id] then
			db.headList[id] = nil
			if db.head == id then
				db.head = DEFAULT_HEAD_MALE_ID
                if human then
                    human.db.head = DEFAULT_HEAD_MALE_ID
                end
			end
			RoleDBLogic.saveRoleSset(db)
			changed = true
		end
	elseif headType == HEAD_TYPE_2 then -- 头像框
		local db = RoleDBLogic.getDb(uuid,{headFrame = 1, headFrameList = 1})
		if db.headFrameList and db.headFrameList[id] then
			db.headFrameList[id] = nil
			if db.headFrame == id then
				db.headFrame = DEFAULT_HEADFREAM_ID
                if human then
                    human.db.headFrame = DEFAULT_HEADFREAM_ID
                end
			end
			RoleDBLogic.saveRoleSset(db)
			changed = true
		end
	elseif headType == HEAD_TYPE_3 then -- 形象
		local db = RoleDBLogic.getDb(uuid,{body = 1, bodyList = 1})
		if db.bodyList and db.bodyList[id] then
			db.bodyList[id] = nil
			if db.body == id then
				db.body = nil
                if human then
                    human.db.body = nil
                end
			end
			RoleDBLogic.saveRoleSset(db)
			changed = true
		end
	elseif headType == HEAD_TYPE_4 then -- 称号
		local db = RoleDBLogic.getDb(uuid,{chenghao = 1, chenghaoList = 1})
		if db.chenghaoList and db.chenghaoList[id] then
			db.chenghaoList[id] = nil
			if db.chenghao == id then
				db.chenghao = nil
                if human then
                    human.db.chenghao = nil
                end
			end
			RoleDBLogic.saveRoleSset(db)
			changed = true
		end
    elseif headType == HEAD_TYPE_5 then -- 立绘
		local db = RoleDBLogic.getDb(uuid,{body = 1, animationList = 1})
		if db.animationList and db.animationList[id] then
			db.animationList[id] = nil
			if db.animation == id then
				db.animation = nil
                if human then
                    human.db.animation = nil
                end
			end
			RoleDBLogic.saveRoleSset(db)
			changed = true
		end
	elseif headType == HEAD_TYPE_6 then -- 背景
		local db = RoleDBLogic.getDb(uuid,{chenghao = 1, backgroundList = 1})
		if db.backgroundList and db.backgroundList[id] then
			db.backgroundList[id] = nil
			if db.background == id then
				db.background = nil
                if human then
                    human.db.background = nil
                end
			end
			RoleDBLogic.saveRoleSset(db)
			changed = true
		end
	end
	
	if changed then
		if human then
            RoleAttr.cleanHeroAttrCache(human)
			local config = getConfig(headType, id)
			if config and #config.attrs > 0 then
				ObjHuman.doCalcHero(human)
			end
            ObjHuman.sendHumanInfo(human)
		end
	end
end

-- 是否有红点
function isDotByType(human, headType)
	if human.db.headHasNewFlag == nil then
		return
	end
	
	local val = human.db.headHasNewFlag or 0
	local bit = Util.getBit(val, headType) 
	if bit ~= 0 then
		return true
	end
end

function setNewFlag(human, type, flag)
	local val = human.db.headHasNewFlag or 0
	if (Util.getBit(val, type) > 0) == flag then
		return 
	end
	
	if flag == true then
		val = Util.setBit(val, type)
	else
		local bit = Util.getBit(val, type) 
		if bit ~= 0 then
			val = val - 2 ^ type
		end
	end
	
	if val == 0 then
		val = nil
	end
	human.db.headHasNewFlag = val
end

function query(human, headType)
	if headType < HEAD_TYPE_1 or headType > HEAD_TYPE_6 then
		return
	end

	local msgRet = Msg.gc.GC_ROLE_HEAD_QUERY
	msgRet.type = headType
	msgRet.headList[0] = 0

	msgRet.isDot[0] = 4
	msgRet.isDot[1] = isDotByType(human, HEAD_TYPE_1) and 1 or 0
	msgRet.isDot[2] = isDotByType(human, HEAD_TYPE_2) and 1 or 0
	msgRet.isDot[3] = isDotByType(human, HEAD_TYPE_3) and 1 or 0
	msgRet.isDot[4] = isDotByType(human, HEAD_TYPE_4) and 1 or 0
	
	if headType == HEAD_TYPE_1 then -- 头像
		for id, cf in pairs(RoleExcel.head) do
            if cf.type ~= DEFAULT_DEL_TYPE then
			   msgRet.headList[0] = msgRet.headList[0] + 1
			   local headNet = msgRet.headList[msgRet.headList[0]]
			   headNet.headID = id 
			   headNet.isActive = isActive(human, HEAD_TYPE_1, id) and 1 or 0	
			   headNet.desc = cf.desc or ""
			   headNet.keepTime = getKeepTime(human, HEAD_TYPE_1, id)
			   headNet.name = cf.name or ""
			   headNet.camp = cf.camp or 0
               local len = #cf.attrs 
               for i = 1,len do
                   headNet.attr[i].key = cf.attrs[i][1]
                   headNet.attr[i].value = cf.attrs[i][2]
               end
               headNet.attr[0] = len
            end
		end
		msgRet.useID = human.db.head or 0
	elseif headType == HEAD_TYPE_2 then -- 头像框
		for id, cf in pairs(RoleExcel.headFrame) do
            if cf.type ~= DEFAULT_DEL_TYPE then
			   msgRet.headList[0] = msgRet.headList[0] + 1
			   local headNet = msgRet.headList[msgRet.headList[0]]
			   headNet.headID = id 
			   headNet.isActive = isActive(human, HEAD_TYPE_2, id) and 1 or 0	
			   headNet.desc = cf.desc or ""
			   headNet.keepTime = getKeepTime(human, HEAD_TYPE_2, id)
			   headNet.name = cf.name or ""
			   headNet.camp = cf.camp or 0
               local len = #cf.attrs 
               for i = 1,len do
                   headNet.attr[i].key = cf.attrs[i][1]
                   headNet.attr[i].value = cf.attrs[i][2]
               end
               headNet.attr[0] = len
            end
		end
		msgRet.useID = human.db.headFrame or 0
	elseif headType == HEAD_TYPE_3 then -- 形象
		for id, cf in pairs(RoleExcel.body) do
            if cf.type ~= DEFAULT_DEL_TYPE then
			   msgRet.headList[0] = msgRet.headList[0] + 1
			   local headNet = msgRet.headList[msgRet.headList[0]]
			   headNet.headID = id 
			   headNet.isActive = isActive(human, HEAD_TYPE_3, id) and 1 or 0	
			   headNet.desc = cf.desc or ""
			   headNet.keepTime = getKeepTime(human, HEAD_TYPE_3, id)
			   headNet.name = cf.name or ""
			   headNet.camp = 0
               local len = #cf.attrs 
               for i = 1,len do
                   headNet.attr[i].key = cf.attrs[i][1]
                   headNet.attr[i].value = cf.attrs[i][2]
               end
               headNet.attr[0] = len
            end
		end
		msgRet.useID = human.db.body or 0
	elseif headType == HEAD_TYPE_4 then -- 称号
		msgRet.headList[0] = 0
		msgRet.useID = 0
		
		ChenghaoImp.query(human)
    elseif headType == HEAD_TYPE_5 then -- 立绘
		for id, cf in pairs(RoleExcel.body) do
            if cf.type ~= DEFAULT_DEL_TYPE then
			   msgRet.headList[0] = msgRet.headList[0] + 1
			   local headNet = msgRet.headList[msgRet.headList[0]]
			   headNet.headID = id 
			   headNet.isActive = isActive(human, HEAD_TYPE_5, id) and 1 or 0	
			   headNet.desc = cf.desc or ""
			   headNet.keepTime = getKeepTime(human, HEAD_TYPE_5, id)
			   headNet.name = cf.name or ""
			   headNet.camp = cf.camp or 0
               headNet.attr[0] = 0
            end
		end
		msgRet.useID = human.db.animation or 0
	elseif headType == HEAD_TYPE_6 then -- 背景
		for id, cf in pairs(RoleExcel.background) do
            if cf.type ~= DEFAULT_DEL_TYPE then
			   msgRet.headList[0] = msgRet.headList[0] + 1
			   local headNet = msgRet.headList[msgRet.headList[0]]
			   headNet.headID = id 
			   headNet.isActive = isActive(human, HEAD_TYPE_6, id) and 1 or 0	
			   headNet.desc = cf.desc or ""
			   headNet.keepTime = getKeepTime(human, HEAD_TYPE_6, id)
			   headNet.name = cf.name or ""
			   headNet.camp = cf.camp or 0
               headNet.attr[0] = 0
            end
		end
		msgRet.useID = human.db.background or 0
	end
	
	setNewFlag(human, headType, nil)
	
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)

	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_101)
end

-- 背景立绘查询
function bgAndLhQuery(human)
	local msgRet = Msg.gc.GC_ROLE_BG_LH_QUERY
    msgRet.data[0] = 2
    local dataNet = msgRet.data[1]
	dataNet.headList[0] = 0
	for id, cf in pairs(RoleExcel.body) do
		dataNet.headList[0] = dataNet.headList[0] + 1
        dataNet.type = HEAD_TYPE_5
		local headNet = dataNet.headList[dataNet.headList[0]]
		headNet.headID = id 
		headNet.isActive = isActive(human, HEAD_TYPE_5, id) and 1 or 0	
		headNet.desc = cf.desc or ""
		headNet.keepTime = getKeepTime(human, HEAD_TYPE_5, id)
		headNet.name = cf.name or ""
		headNet.camp = cf.camp or 0
        headNet.attr[0] = 0
	    dataNet.useID = human.db.animation or 0
	end
    setNewFlag(human, HEAD_TYPE_5, nil)
    dataNet = msgRet.data[2]
	dataNet.headList[0] = 0
	for id, cf in pairs(RoleExcel.background) do
		dataNet.headList[0] = dataNet.headList[0] + 1
		local headNet = dataNet.headList[dataNet.headList[0]]
        dataNet.type = HEAD_TYPE_6
		headNet.headID = id 
		headNet.isActive = isActive(human, HEAD_TYPE_6, id) and 1 or 0	
		headNet.desc = cf.desc or ""
		headNet.keepTime = getKeepTime(human, HEAD_TYPE_6, id)
		headNet.name = cf.name or ""
		headNet.camp = cf.camp or 0
        headNet.attr[0] = 0
	    dataNet.useID = human.db.background or 0
	end
    setNewFlag(human, HEAD_TYPE_6, nil)
	
	Msg.send(msgRet, human.fd)

	RoleSystemLogic.onDot(human, RoleSystemDefine.ROLE_SYS_ID_101)
end



-- 设置头像/头像框/形象
function setHead(human, headID, headType)
	if headType < HEAD_TYPE_1 or headType > HEAD_TYPE_6 then
		return
	end

	if isActive(human, headType, headID) ~= true then
		return
	end

	local changed = nil
	local useID = nil
	if headType == HEAD_TYPE_1 then -- 头像
		useID = human.db.head or 0
		if useID == headID then
			return
		end
		human.db.head = headID
		changed = true
	elseif headType == HEAD_TYPE_2 then -- 头像框
		useID = human.db.headFrame or 0
		if useID == headID then
			return
		end
		human.db.headFrame = headID
		changed = true
	elseif headType == HEAD_TYPE_3 then -- 形象
		useID = human.db.body or 0
		if useID == headID then
			return
		end
		human.db.body = headID
		changed = true
        -- 查询是否有占领王座，如有占领，更改形象
        local list = TheStarsDBLogic.getThroneList()
        for i, v in ipairs(list) do
            if v.uuid ~= nil and v.uuid == human.db._id then
                v.body = headID
                TheStarsDBLogic.updateThroneDB(v)
                break
            end
        end
	elseif headType == HEAD_TYPE_4 then -- 称号
		useID = human.db.chenghao or 0
		if useID == headID then
			return
		end
		human.db.chenghao = headID
		changed = true
    elseif headType == HEAD_TYPE_5 then -- 立绘
		useID = human.db.animation or 0
		if useID == headID then
			return
		end
		human.db.animation = headID
		changed = true
	elseif headType == HEAD_TYPE_6 then -- 背景
		useID = human.db.background or 0
		if useID == headID then
			return
		end
		human.db.background = headID
		changed = true
	end
	
    RoleAttr.cleanHeroAttrCache(human)

	if changed == true then
		-- 通知客户端
		local msgRet = Msg.gc.GC_ROLE_HEAD_SET
		msgRet.headID = headID
		msgRet.headType = headType
		Msg.send(msgRet, human.fd)

		--同步头像设置
        if headType == HEAD_TYPE_5 or headType == HEAD_TYPE_6 then -- 立绘
            bgAndLhQuery(human)
        else
		    query(human, headType)
        end

		if headType == HEAD_TYPE_1 then -- 头像
			HuanJingTowerLogic.changeHeadAfter(human)	
		end
	end
end

function getKeepTime(human, headType, id)
    local config = nil
	local checkDB = nil
	if headType == HEAD_TYPE_1 then -- 头像
        if id == DEFAULT_HEAD_MALE_ID or 
			id == DEFAULT_HEAD_FEMALE_ID then
			return -1
		end

        config = RoleExcel.head[id]
        if config == nil then
            return 0
        end

		if not human.db.headList then
		    return config.keepTime == -1 and config.keepTime or config.keepTime*60*60
        end	

		checkDB = human.db.headList[id]
	elseif headType == HEAD_TYPE_2 then -- 头像框
        if id == DEFAULT_HEADFREAM_ID then
			return -1
		end
        
        config = RoleExcel.headFrame[id]
        if config == nil then
            return 0
        end

		if not human.db.headFrameList then
			return config.keepTime == -1 and config.keepTime or config.keepTime*60*60
		end	
		checkDB = human.db.headFrameList[id]
	elseif headType == HEAD_TYPE_3 then -- 形象
        
        config = RoleExcel.body[id]
        if config == nil then
            return 0
        end

        if not human.db.bodyList then
			return config.keepTime == -1 and config.keepTime or config.keepTime*60*60
		end	
		checkDB = human.db.bodyList[id]
	elseif headType == HEAD_TYPE_4 then -- 称号

        config = RoleExcel.chenghao[id]
        if config == nil then
            return 0
        end

        if not human.db.chenghaoList then
			return config.keepTime == -1 and config.keepTime or config.keepTime*60*60
		end	

		checkDB = human.db.chenghaoList[id]
    elseif headType == HEAD_TYPE_5 then -- 立绘
        
        config = RoleExcel.body[id]
        if config == nil then
            return 0
        end

        if not human.db.animationList then
			return config.keepTime == -1 and config.keepTime or config.keepTime*60*60
		end	
		checkDB = human.db.animationList[id]
	elseif headType == HEAD_TYPE_6 then -- 背景

        config = RoleExcel.background[id]
        if config == nil then
            return 0
        end

        if not human.db.backgroundList then
			return config.keepTime == -1 and config.keepTime or config.keepTime*60*60
		end	

		checkDB = human.db.backgroundList[id]
	end
	
	if checkDB == nil then
		return 0
	end
	
	if checkDB == true then
		return -1
	end
	
    if checkDB.keepTime < 0 then
        return -1
    end

    local now = os.time()
	if checkDB.ts + checkDB.keepTime <= now then
		return 0
	else
        return checkDB.ts + checkDB.keepTime  - now
	end
end

-- 获得英雄回调
function onAddHero(human, heroID)
	local heroConfig = HeroExcel.hero[heroID] 
	active(human, HEAD_TYPE_1, heroConfig.head)
	active(human, HEAD_TYPE_3, heroConfig.body)
	active(human, HEAD_TYPE_5, heroConfig.body)
end

function isDot(human)
	if human.db.headHasNewFlag then
		return true
	end
end

function CG_ROLE_COMBATHERO_QUERY(human, type)
	local combatType = type
	local combatHero,helpList = CombatPosLogic.getCombatHeros(human, combatType, nil, true)
    if not combatHero then return end

	local msgRet = Msg.gc.GC_ROLE_COMBATHERO_QUERY
	msgRet.combatType = combatType
	msgRet.titleId = human.db.chenghao or 0
	msgRet.totalcombat = CombatPosLogic.getCombatHeroZDL(human, combatType, true)
	--msgRet.bagIndex[0] = CombatDefine.COMBAT_HERO_CNT
    local len = 0
    for k,v in pairs(combatHero) do
        local heroGrid = HeroLogic.getHeroGridByUuid(human, v)
		if heroGrid then
			len = len + 1
			msgRet.bagIndex[len] = heroGrid.bagIndex
		end
    end
	for k,v in pairs(helpList) do
        local heroGrid = HeroLogic.getHeroGridByUuid(human, v)
		if heroGrid then
			len = len + 1
			msgRet.bagIndex[len] = heroGrid.bagIndex
		end
    end
    msgRet.bagIndex[0] = len
    --Msg.trace(msgRet)
    Msg.send(msgRet, human.fd)    
end

function doCalcHero(human, attrs)
	if not human then return end
	
	if human.db.headList then
		for k, v in pairs(human.db.headList) do
			if isActive(human, HEAD_TYPE_1, k) and human.db.head == k then
				local config = getConfig(HEAD_TYPE_1, k)
				for k1,v1 in ipairs(config.attrs) do
					RoleAttr.updateValue(v1[1], v1[2], attrs)
				end
			end
		end
	end	
	
	if human.db.headFrameList then
		for k, v in pairs(human.db.headFrameList) do
			if isActive(human, HEAD_TYPE_2, k) and human.db.headFrame == k then
				local config = getConfig(HEAD_TYPE_2, k)
				for k1,v1 in ipairs(config.attrs) do
					RoleAttr.updateValue(v1[1], v1[2], attrs)
				end
			end
		end
	end	
	
	if human.db.bodyList then
		for k, v in pairs(human.db.bodyList) do
			if isActive(human, HEAD_TYPE_3, k) and human.db.body == k then
				local config = getConfig(HEAD_TYPE_3, k)
				for k1,v1 in ipairs(config.attrs) do
					RoleAttr.updateValue(v1[1], v1[2], attrs)
				end
			end
		end
	end	
	
	if human.db.chenghaoList then
		for k, v in pairs(human.db.chenghaoList)  do
			if isActive(human, HEAD_TYPE_4, k) then
				local config = getConfig(HEAD_TYPE_4, k)
				for k1,v1 in ipairs(config.attrs) do
					RoleAttr.updateValue(v1[1], v1[2], attrs)
				end
			end
		end
	end	

    if human.db.animationList then
		for k, v in pairs(human.db.animationList) do
			if isActive(human, HEAD_TYPE_5, k) and human.db.animation == k then
				local config = getConfig(HEAD_TYPE_5, k)
				for k1,v1 in ipairs(config.attrs) do
					RoleAttr.updateValue(v1[1], v1[2], attrs)
				end
			end
		end
	end

    if human.db.backgroundList then
		for k, v in pairs(human.db.backgroundList) do
			if isActive(human, HEAD_TYPE_6, k) then
				local config = getConfig(HEAD_TYPE_6, k)
				for k1,v1 in ipairs(config.attrs) do
					RoleAttr.updateValue(v1[1], v1[2], attrs)
				end
			end
		end
	end
end


